import cv2
# 首先载入级联的模型
import time
face = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
# 设置文件夹保存照片的路径
path = './faces/train/zhicheng'

cap = cv2.VideoCapture(0)
capture_count = 300
i = 0
while True:
    # 这里read返回的值是两个一个是是否读取成功，一个是矩阵数组
    ret, img = cap.read()
    img = cv2.resize(img, None, fx=1, fy=1)
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    face_area = face.detectMultiScale(img, 1.5, 5)
    print(1)
    print(face_area)
    for (x, y, w, h) in face_area:
        cv2.imwrite(path + str(i) + '.jpg', img_gray)
        i += 1
        print('正在采集' + str(i) + '张照片')
    if i >= capture_count:
        break
    cv2.imshow('img', img)
    time.sleep(0.01)
    # waitkey设置为1时候可以接受键盘的返回值
    key = cv2.waitKey(1)
    if key == ord('q'):
        break
